package com.hdj.shuzu;

import java.util.*;

/**
 * @Author: HeDongJia
 * @Description:
 * @Date: Create in 22:47 2021/7/19
 * @Modified By:
 */
public class ListAndArray切换 {
    public void Array转List(){
        String[] arrs={"zhangsan","lisi","wangwu"};
        //循环转换
        List<String> list1 = new ArrayList<>();
        for (String arr:arrs){
            list1.add(arr);
        }
        System.out.println(list1);
        // 借助数组工具类
        List<String> list2 = Arrays.asList(arrs);
        System.out.println(list2);
        //借助集合工具类
        List<String> list3 = new ArrayList<>();
        Collections.addAll(list3,arrs);
        System.out.println(list3);
    }

    public void List切换Array(){
        List<String> list = Arrays.asList("1","2");
        String[] arr1 = new String[list.size()];
        // 循环遍历赋值
        for (int i = 0; i < list.size(); i++) {
            arr1[i] = list.get(i);
        }
        // 调用list 方法
        String[] arr2 = (String[]) list.toArray();
    }
    public static void main(String[] args) {
        new ListAndArray切换().Array转List();
    }
}
